﻿namespace WMS6.Web.Outbound
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;
    using WMS6.Web.OutboundServiceRef;

    public class ShippingWeight : BasePage
    {
        protected Button btnCancelOrder;
        protected Button btnDeliveryOrder;
        protected Button btnGoBack;
        protected HtmlAnchor btnPrompt;
        protected HtmlAnchor btnRealWeight;
        protected Button btnUpdatePackingType;
        protected CheckBox chkDeliveryPrint;
        protected CheckBox chkScanPack;
        protected HtmlForm F7091;
        protected GridView gvWeightCancelReview;
        protected GridView gvWeightReview;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnOrderID;
        protected HtmlInputHidden hdnOwnerID;
        protected HtmlInputHidden hdnRealWeight;
        protected HtmlInputHidden hdnSetDeliveryOrder;
        protected HtmlInputHidden hdnWHID;
        protected HtmlHead Head1;
        protected Label lblASN;
        protected Label lblBeginDeliveryOrder;
        protected Label lblCarrierID;
        protected Label lblCurrentDeliveryOrder;
        protected Label lblDifferencesWtg;
        protected Label lblEndDeliveryOrder;
        protected Label lblMsg;
        protected Label lblOrderID;
        protected Label lblPackingType;
        protected Label lblRealWeight;
        protected Label lblSysWeight;
        protected HtmlLink mycss;
        protected ScriptManager ScriptManager1;
        protected TextBox txtBeginDeliveryOrder;
        protected TextBox txtCarrierID;
        protected TextBox txtCurrentDeliveryOrder;
        protected TextBox txtDifferencesWtg;
        protected TextBox txtEndDeliveryOrder;
        protected TextBox txtOrderID;
        protected TextBox txtPackingType;
        protected TextBox txtRealWeight;
        protected TextBox txtSysWeight;
        protected UpdatePanel UpdatePanel1;

        protected void btnCancelOrder_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvWeightCancelReview, "OrderID");
            if ((selectedID == null) && (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C008");
            }
            else
            {
                try
                {
                    string stemp = OutboundServiceClientProxy.GetInstance().ShippingWeightCancelOrder(selectedID);
                    this.lblMsg.Text = stemp + "取消完成";
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = "操作异常:" + ex.Message;
                }
            }
        }

        protected void btnPrompt_Click(object sender, EventArgs e)
        {
            List<OrdersInfo> ordersInfoList = this.ViewState["ShippingWeightList"] as List<OrdersInfo>;
            List<OrdersInfo> ordersInfoCancelList = this.ViewState["ShippingWeightCancelList"] as List<OrdersInfo>;
            OrdersInfo ordersinfo = this.ViewState["ShippingWeight"] as OrdersInfo;
            try
            {
                if (ordersinfo.IsICancel != "Y")
                {
                    if (Convert.ToInt32(this.txtCurrentDeliveryOrder.Text) > Convert.ToInt32(this.txtEndDeliveryOrder.Text))
                    {
                        this.lblMsg.Text = "本次运单号已经全部用完,请更换运单号!";
                    }
                    else
                    {
                        this.txtCurrentDeliveryOrder.Text = (int.Parse(this.txtCurrentDeliveryOrder.Text) + 1).ToString();
                    }
                    ordersInfoList.Add(ordersinfo);
                    OutboundServiceClientProxy.GetInstance().UpdateOrdersByShippingWeight(ordersinfo);
                    this.ViewState["ShippingWeightList"] = ordersInfoList;
                    base.BindData2GridView(this.gvWeightReview, this.SetGvidViewRow(ordersInfoList, 15));
                }
                else
                {
                    ordersInfoCancelList.Add(ordersinfo);
                    this.ViewState["ShippingWeightCancelList"] = ordersInfoCancelList;
                    base.BindData2GridView(this.gvWeightCancelReview, this.SetGvidViewRow(ordersInfoCancelList, 5));
                }
                this.ViewState["ShippingWeight"] = "";
                this.CleanPage();
            }
            catch (Exception ex)
            {
                ordersInfoList.Remove(ordersinfo);
                this.lblMsg.Text = "操作异常:" + ex.Message;
            }
        }

        protected void btnRealWeigh_Click(object sender, EventArgs e)
        {
            List<OrdersInfo> ordersInfoList;
            OrdersInfo ordersinfo;
            if (string.IsNullOrEmpty(this.txtOrderID.Text))
            {
                this.txtOrderID.Focus();
            }
            else if (string.IsNullOrEmpty(this.txtPackingType.Text))
            {
                this.txtPackingType.Focus();
            }
            else
            {
                if (string.IsNullOrEmpty(this.txtRealWeight.Text))
                {
                    this.lblMsg.Text = "请输入包裹实际重量!";
                    this.txtRealWeight.Focus();
                    return;
                }
                List<OrdersInfo> ordersInfoCancelList = this.ViewState["ShippingWeightCancelList"] as List<OrdersInfo>;
                ordersInfoList = this.ViewState["ShippingWeightList"] as List<OrdersInfo>;
                ordersinfo = this.ViewState["ShippingWeight"] as OrdersInfo;
                if (ordersinfo == null)
                {
                    goto Label_0342;
                }
                decimal SysWeight = decimal.Parse(this.txtSysWeight.Text);
                decimal RealWeight = decimal.Parse(this.txtRealWeight.Text);
                decimal differencesWtg = SysWeight - RealWeight;
                this.txtDifferencesWtg.Text = base.GetPreferenceDigit(differencesWtg);
                ordersinfo.PackingType = this.txtPackingType.Text;
                ordersinfo.WaybillNo = this.txtCurrentDeliveryOrder.Text;
                ordersinfo.ActualWeight = decimal.Parse(this.txtRealWeight.Text);
                if (!(differencesWtg != 0M))
                {
                    goto Label_0273;
                }
                TradePartnerInfo ownerInfo = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(this.hdnOwnerID.Value, "TRADE_PARTNER") as TradePartnerInfo;
                if (ownerInfo == null)
                {
                    this.lblMsg.Text = "单据货主设置异常,请核对!";
                    return;
                }
                if (!(ownerInfo.CheckTheWeightDifference == "Y"))
                {
                    goto Label_0273;
                }
                if (SysWeight == 0M)
                {
                    SysWeight = 1M;
                }
                if ((differencesWtg / SysWeight) > ownerInfo.PercentageWeightDifference)
                {
                    goto Label_0273;
                }
                string CS40001 = ownerInfo.DifferencesInApproach;
                if (CS40001 == null)
                {
                    goto Label_0273;
                }
                if (!(CS40001 == "Prompt"))
                {
                    if (CS40001 == "Forbid")
                    {
                        this.lblMsg.Text = "实际包裹重量和系统包裹计重差异超出范围，不允许继续发货.";
                        return;
                    }
                    goto Label_0273;
                }
                this.ViewState["ShippingWeight"] = ordersinfo;
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>Prompt();</script>", false);
            }
            return;
        Label_0273:
            if (Convert.ToInt32(this.txtCurrentDeliveryOrder.Text) > Convert.ToInt32(this.txtEndDeliveryOrder.Text))
            {
                this.lblMsg.Text = "本次运单号已经全部用完,请更换运单号!";
            }
            else
            {
                this.txtCurrentDeliveryOrder.Text = (int.Parse(this.txtCurrentDeliveryOrder.Text) + 1).ToString();
            }
            ordersInfoList.Add(ordersinfo);
            OutboundServiceClientProxy.GetInstance().UpdateOrdersByShippingWeight(ordersinfo);
            this.ViewState["ShippingWeightList"] = ordersInfoList;
            base.BindData2GridView(this.gvWeightReview, this.SetGvidViewRow(ordersInfoList, 15));
            this.ViewState["ShippingWeight"] = "";
            this.CleanPage();
            this.txtPackingType.Focus();
        Label_0342:
            if (this.chkDeliveryPrint.Checked)
            {
                this.hdnWHID.Value = base.UserInfo.CurrentWH;
                this.hdnOrderID.Value = ordersinfo.OrderID;
                this.hdnOwnerID.Value = ordersinfo.OwnerID;
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>doPrintView();</script>", false);
            }
        }

        protected void btnUpdatePackingType_Click(object sender, EventArgs e)
        {
            List<string> selectID = new List<string>();
            if (!string.IsNullOrEmpty(this.txtPackingType.Text))
            {
                for (int i = 0; i < this.gvWeightReview.Rows.Count; i++)
                {
                    if (((CheckBox) this.gvWeightReview.Rows[i].FindControl("chbSelect")).Checked)
                    {
                        selectID.Add(this.gvWeightReview.DataKeys[i]["OrderID"].ToString());
                        ((Label) this.gvWeightReview.Rows[i].FindControl("lblPackingType")).Text = this.txtPackingType.Text;
                    }
                }
                string stemp = OutboundServiceClientProxy.GetInstance().ShippingWeightCancelOrder(selectID);
            }
            else
            {
                this.lblMsg.Text = "包材型号不能为空!";
            }
        }

        private void CleanPage()
        {
            this.txtCarrierID.Text = "";
            this.txtDifferencesWtg.Text = "";
            this.txtOrderID.Text = "";
            this.txtPackingType.Text = "";
            this.txtRealWeight.Text = "";
            this.txtSysWeight.Text = "";
            this.txtOrderID.Focus();
        }

        protected void gvWeightCancelReview_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (string.IsNullOrEmpty(e.Row.Cells[2].Text) || (e.Row.Cells[2].Text == "&nbsp;"))
                {
                    ((Label) e.Row.FindControl("lblPrintCount")).Text = "";
                }
            }
        }

        protected void gvWeightReview_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (string.IsNullOrEmpty(e.Row.Cells[2].Text) || (e.Row.Cells[2].Text == "&nbsp;"))
                {
                    ((Label) e.Row.FindControl("lblActualWeight")).Text = "";
                    ((Label) e.Row.FindControl("lblPrintCount")).Text = "";
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.ViewState["ShippingWeightList"] = new List<OrdersInfo>();
                base.BindData2GridView(this.gvWeightReview, this.SetGvidViewRow(new List<OrdersInfo>(), 15));
                this.ViewState["ShippingWeightCancelList"] = new List<OrdersInfo>();
                base.BindData2GridView(this.gvWeightCancelReview, this.SetGvidViewRow(new List<OrdersInfo>(), 5));
                this.txtRealWeight.Attributes.Add("onblur", "CalculatedWeight();");
                this.txtCurrentDeliveryOrder.Attributes.Add("onblur", "RealWeight();");
                this.txtPackingType.Attributes.Add("onblur", "RealWeight();");
                this.txtBeginDeliveryOrder.Attributes.Add("onblur", "SetDeliveryOrder(this);");
                this.txtEndDeliveryOrder.Attributes.Add("onblur", "SetEndDeliveryOrder(this);");
                this.txtPackingType.Attributes.Add("onblur", "SetPackingType(this);");
                this.txtBeginDeliveryOrder.Focus();
            }
            this.lblMsg.Text = "";
        }

        private List<OrdersInfo> SetGvidViewRow(List<OrdersInfo> ordersInfoList, int rowCount)
        {
            List<OrdersInfo> newOrderList = new List<OrdersInfo>();
            foreach (OrdersInfo info in ordersInfoList)
            {
                newOrderList.Add(info);
            }
            for (int i = newOrderList.Count<OrdersInfo>(); i < rowCount; i++)
            {
                OrdersInfo ordersinfo = new OrdersInfo();
                newOrderList.Add(ordersinfo);
            }
            return newOrderList;
        }

        protected void txtOrderID_OnTextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtBeginDeliveryOrder.Text))
            {
                this.lblMsg.Text = "请先设置起始运单号!";
                this.txtBeginDeliveryOrder.Focus();
                this.txtOrderID.Text = "";
            }
            else if (string.IsNullOrEmpty(this.txtEndDeliveryOrder.Text))
            {
                this.lblMsg.Text = "请先设置结束运单号!";
                this.txtEndDeliveryOrder.Focus();
                this.txtOrderID.Text = "";
            }
            else if (!string.IsNullOrEmpty(this.txtOrderID.Text))
            {
                List<OrdersInfo> ordersInfoList = this.ViewState["ShippingWeightList"] as List<OrdersInfo>;
                List<OrdersInfo> ordersInfoCancelList = this.ViewState["ShippingWeightCancelList"] as List<OrdersInfo>;
                foreach (OrdersInfo info in ordersInfoList)
                {
                    if (info.OrderID == this.txtOrderID.Text.Trim())
                    {
                        this.lblMsg.Text = this.txtOrderID.Text + "单据已经在列表内存在,无法重复记录!";
                        return;
                    }
                }
                foreach (OrdersInfo info in ordersInfoCancelList)
                {
                    if (info.OrderID == this.txtOrderID.Text.Trim())
                    {
                        this.lblMsg.Text = this.txtOrderID.Text + "单据已经在列表内存在,无法重复记录!";
                        return;
                    }
                }
                OrdersInfo orderInfo = OutboundServiceClientProxy.GetInstance().GetShippingWeightByOrderID(this.txtOrderID.Text);
                if (orderInfo != null)
                {
                    if (orderInfo.Status.ToUpper() != "REVIEWED")
                    {
                        this.lblMsg.Text = "单据状态不等于复核完成,无法进行称重!";
                        return;
                    }
                    if (orderInfo.IsICancel == "Y")
                    {
                        ordersInfoCancelList.Add(orderInfo);
                        this.ViewState["ShippingWeightCancelList"] = ordersInfoCancelList;
                        base.BindData2GridView(this.gvWeightCancelReview, this.SetGvidViewRow(ordersInfoCancelList, 5));
                        this.CleanPage();
                        return;
                    }
                    this.txtOrderID.Text = orderInfo.OrderID;
                    this.hdnOwnerID.Value = orderInfo.OwnerID;
                    this.txtSysWeight.Text = base.GetPreferenceDigit(orderInfo.TotalWgt);
                    this.txtCarrierID.Text = orderInfo.CarrierID;
                    this.txtPackingType.Attributes.Add("onchange", "RealWeight();");
                    this.txtPackingType.Focus();
                    this.ViewState["ShippingWeight"] = orderInfo;
                    this.txtRealWeight.Focus();
                }
                else
                {
                    this.lblMsg.Text = "未找到匹配单据!";
                    this.txtOrderID.Text = "";
                    this.txtOrderID.Focus();
                }
                if (!(((string.IsNullOrEmpty(this.txtOrderID.Text) || string.IsNullOrEmpty(this.txtRealWeight.Text)) || string.IsNullOrEmpty(this.txtCurrentDeliveryOrder.Text)) || string.IsNullOrEmpty(this.txtPackingType.Text)))
                {
                    this.btnRealWeigh_Click(null, null);
                }
            }
        }
    }
}

